home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
onkey.exe
/
ONKEY.TXT
< prev
Wrap
Text File
|
1994-10-27
|
9KB
|
306 lines
ONKEY
This ZIP file contains the following members :
ONKEY.H - An include file that prototypes the various functions in
ONKEY.LIB and #defines various key codes.
ONKEY.LIB - A LARGE Model Library of 'C' functions described below
The functions in ONKEY.LIB are described in detail below. The functions
in this Library are compiled foe the large model and a 286 or better
processor.
int OnKey(key_code,(function)());
int key_code; scan code, ascii code of key (see onkey.h)
void (function)(); function to call when key is pressed
Description.
After a successful call to this function whenever the key described by
key_code is pressed the associated function is invoked. Passing a function
of NULL causes the key to be treated as normal.
The include file onkey.h contains examples of the key codes for some keys.
The key codes are made up of the scan code and the ascii character and
represent the value returned by the BIOS INT 16H function 10H call.
Example.
void ProcFN1(void);
void ProcFNA1(void);
main( )
{
OnKey(F1,ProcFN1); // call ProcFN1 whenever F1 is pressed
OnKey(ALT_F1,ProcFNA1); // call ProcFNA1 whenever ALT F1 is pressed
InKey(0); // wait for a key to be pressed
OnKey(F1,NULL); // treat F1 as a normal key
}
ProcFN1( )
{
Printf("FN1 Was Pressed\n");
}
ProcFNA1( )
{
Printf("FNA1 Was Pressed\n");
}
Return Value.
returns 0 if works, -1 if fails. THe only reason to fail is lack of memory.
The example above uses InKey( ) to retrieve a key. The OnKey( ) function
actually hooks into INT 16H and will invoke the correspnding function
regardless of what C function you use to get keyboard input.
Please be aware though that C does stack checking and it is very possible
that you will get a run time error. i.e the following code will give a
stack overflow message :
void ProcFN1(void);
void ProcFNA1(void);
main( )
{
int num;
OnKey(F1,ProcFN1); // call ProcFN1 whenever F1 is pressed
OnKey(ALT_F1,ProcFNA1); // call ProcFNA1 whenever ALT F1 is pressed
printf("Please enter a number : ");
scanf("%d",&num);
OnKey(F1,NULL); // treat F1 as a normal key
}
ProcFN1( )
{
Printf("FN1 Was Pressed\n");
}
ProcFNA1( )
{
Printf("FNA1 Was Pressed\n");
}
Pressing F1 or ALT F1 will cause a stack overflow message to be generated.
Compiling your program with stack checking disabled (/Gs) will not help in
the above example because printf already has stack checking embedded within
it. You have three options to prevent this from happening.
1. Only use the Onkey( ) function in combination with Inkey( ).
2. Compile your programs with the /Gs option and don't use any supplied
C library routines in the invoked function.
3. Write your own keyboard routine that uses BIOS and not DOS.
The /Gs option is used with the Microsoft C compiler. I assume that other
C Compilers have a similar option.
int OnKeyCancel(void);
Description.
Cancels all active Onkey( ) calls. You would use this before spawning in
overlay mode another executeable. This call makes sure that all interrupts
that the onkey( ) has 'hooked' into are reset.
Example.
void ProcFN1(void);
void ProcFNA1(void);
main( )
{
OnKey(F1,ProcFN1); // call ProcFN1 whenever F1 is pressed
OnKey(ALT_F1,ProcFNA1); // call ProcFNA1 whenever ALT F1 is pressed
InKey(0); // wait for a key to be pressed
OnKeyCancel( ); // Reset all keys to noraml
}
ProcFN1( )
{
Printf("FN1 Was Pressed\n");
}
ProcFNA1( )
{
Printf("FNA1 Was Pressed\n");
}
Return Value.
returns 0 if works, -1 if fails.
int InKey(delay);
int delay;
Description.
Inkey waits delay seconds for a key to be pressed before returning to
the calling program. A delay value of 0 causes inkey( ) to wait
until a key is actually pressed.
Example.
main( )
{
int key;
key = InKey(0); // wait for a key to be pressed
key = Inkey(5); // wait 5 seconds for a key to be pressed
}
Return Value.
returns 0 if no key was pressed within the specified delay time otherwise
it returns the scan code + ascii character of the key pressed. (See ONKEY.H
for the values;
int LastKey(void);
Description.
Returns the scan code, ascii character of the last key pressed. This function
only works if a successful Onkey or an Inkey( ) call has been made.
Example.
main( )
{
int key;
key = InKey(0); // wait for a key to be pressed
....
..
..
..
if ( LastKey( ) == HOME)
{
.....
}
}
Return Value.
returns the scan code, ascii character of the last key pressed.
int PutKey(key_code);
int key_code; // scan code, ascii character to 'stuff'
Description.
This function 'stuffs' the passed keystroke into the keyboard buffer.
See ONKEY.H for some valid key_codes.
This function may not work on some older machines.
Example.
main( )
{
int key;
OnKey(F1,ProcF1); // hot key F1
key = InKey(0); // wait for a key to be pressed
if ( lastkey( ) == ENTER)
printf("Enter was Pressed\n");
}
ProcF1( )
{
PutKey(ENTER); // put an enter key in the buffer
}
pressing either F1 or Enter will cause the 'Enter was pressed' message
to be displayed.
Return Value.
returns -1 if the keyboard buffer is full else returns 0
To use these functions in your programs compile them as follows :
CL /AL /G2s /c xxxxx.c
link xxxxx,,, onkey;
or
cl /AL /G2s xxxxx.c /link onkey
These functions are provided as is, you use them at your own risk. If
you have any questions or would like source or librarys for other
memory models please contact me at the CIS mail address below.
Steve Bridges.
CIS 71507,1033
----------------end-of-author's-documentation---------------
Software Library Information:
This disk copy provided as a service of
Public (software) Library
We are not the authors of this program, nor are we associated
with the author in any way other than as a distributor of the
program in accordance with the author's terms of distribution.
Please direct shareware payments and specific questions about
this program to the author of the program, whose name appears
elsewhere in this documentation. If you have trouble getting
in touch with the author, we will do whatever we can to help
you with your questions. All programs have been tested and do
run. To report problems, please use the form that is in the
file PROBLEM.DOC on many of our disks or in other written for-
mat with screen printouts, if possible. PsL cannot debug pro-
programs over the telephone, though we can answer questions.
Disks in the PsL are updated monthly, so if you did not get
this disk directly from the PsL, you should be aware that the
files in this set may no longer be the current versions. Also,
if you got this disk from another vendor and are having prob-
lems, be aware that some files may have become corrupted or
lost by that vendor. Get a current, working disk from PsL.
For a copy of the latest monthly software library newsletter
and a li